#include <stdlib.h>
#include <gtk/gtk.h>

#include "currency_box.h"
#define PATH_TO_RESOURCE "/home/projects/eclipse-cpp/gcurrency/%s"

char* find_resource_file (const char *filename, GError **error) {
	FILE *file = fopen (filename, "rb");
	if (!file) {
		char *path = (char *)malloc (100);
		sprintf (path, PATH_TO_RESOURCE, filename);
		file = fopen (path, "rb");
		if (file) {
			fclose (file);
			return path;
		}
		free (path);
		return NULL;
	}
	fclose (file);
	return (char *)filename;
}

gboolean winOnExposeEvent (GtkWidget *wg, GdkEventExpose *ev, gpointer data) {
  GdkPixbuf *bg, *wg_pixbuf, *bg_scaled;
  GError *err = NULL;
  int wg_w, wg_h;

  //get our pixbuf
  const char *file = find_resource_file ("resource/drag.png", &err);
  bg = gdk_pixbuf_new_from_file (file, &err);
  if (!bg) {
	printf("Error message: %s\n", err->message);
	return FALSE;
  }
  //get the draw area information
  gdk_window_get_size (wg->window, &wg_w, &wg_h);
  //printf ("\nSize: %d, %d", wg_w, wg_h);
  //compose the view of widget and the background image
  //--- first, get the current view
  wg_pixbuf = gdk_pixbuf_get_from_drawable (
		  NULL, wg->window,
		  NULL,
		  0, 0, 0, 0,
		  wg_w, wg_h);
  //--- second, scale the background image
  bg_scaled = gdk_pixbuf_scale_simple (bg, wg_w, wg_h, GDK_INTERP_BILINEAR);
  g_object_unref (bg);
  //--- third, compose
  gdk_pixbuf_composite (
		  bg_scaled, wg_pixbuf,
		  0, 0, wg_w, wg_h,
		  0, 0,
		  1, 1,
		  GDK_INTERP_NEAREST,
		  100);
  g_object_unref (bg_scaled);
  //--- forth, draw to the window area
  gdk_draw_pixbuf (
		  wg->window, NULL,
		  wg_pixbuf,
		  0, 0, 0, 0,
		  -1, -1,
		  GDK_RGB_DITHER_NONE,
		  0, 0);
  g_object_unref (wg_pixbuf);

  return FALSE;
}

int main (int argc, char *argv[])
{
  GtkWidget *win, *vbox, *source, *target, *btn_convert;

  /* Initialize GTK+ */
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  gtk_init (&argc, &argv);
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

  /* Create the main window */
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (win), 20);
  gtk_window_set_title (GTK_WINDOW (win), "World Currency Converter");
  gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
  g_signal_connect (win, "destroy",
		  G_CALLBACK (gtk_main_quit), NULL);
  //draw the background for the window
  g_signal_connect_after (win, "expose-event",
		  G_CALLBACK (winOnExposeEvent), NULL);
  /* Create a vertical box with buttons */
  vbox = gtk_vbox_new (FALSE, 3);
  gtk_container_add (GTK_CONTAINER (win), vbox);

  source = currency_box_new ();
  gtk_box_pack_start (GTK_BOX (vbox), source, TRUE, FALSE, 0);
  target = currency_box_new ();
  gtk_box_pack_start (GTK_BOX (vbox), target, TRUE, FALSE, 0);

  btn_convert = gtk_button_new_with_mnemonic ("_Convert");
  gtk_box_pack_end (GTK_BOX (vbox), btn_convert, FALSE, FALSE, 0);

  gtk_widget_show_all (win);
  gtk_main ();
  return EXIT_SUCCESS;
}
